package com.thinkgem.jeesite.modules.borrow.web;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.persistence.Result;
import com.thinkgem.jeesite.common.utils.DateUtils;
import com.thinkgem.jeesite.common.utils.RandomNumUtiles;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.utils.excel.ExportExcel;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.book.entity.BkBookstore;
import com.thinkgem.jeesite.modules.book.service.BookStoreService;
import com.thinkgem.jeesite.modules.borrow.entity.Borrow;
import com.thinkgem.jeesite.modules.borrow.entity.BorrowLog;
import com.thinkgem.jeesite.modules.borrow.entity.Reback;
import com.thinkgem.jeesite.modules.borrow.service.BorrowService;
import com.thinkgem.jeesite.modules.reader.entity.Reader;
import com.thinkgem.jeesite.modules.reader.entity.ReaderRolePlace;
import com.thinkgem.jeesite.modules.reader.entity.ReaderRoler;
import com.thinkgem.jeesite.modules.reader.service.ReaderRolerService;
import com.thinkgem.jeesite.modules.reader.service.ReadersService;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * 借阅管理Controller
 * @author zhangchong
 * @version 2013-8-29
 */
@Controller
@RequestMapping(value = "${adminPath}/book/borrowed")
public class BorrowController extends BaseController {
	@Autowired
	private BookStoreService bookStoreService;

	@Autowired
	private ReadersService readerService;

	@Autowired
	private BorrowService borrowService;

	@Autowired
	private ReaderRolerService readerRolerService;

	@ModelAttribute
	public Borrow get(@RequestParam(required=false) String id) {
		if (StringUtils.isNotBlank(id)){
			return borrowService.get(id);
		}else{
			return new Borrow();
		}
	}


	/**
	 * 借阅管理
	 *
	 * @return
	 */
	@RequestMapping(value = "management")
	public String management(){
		return "modules/borrow/management";
	}

	/**
	 * 借阅查询
	 *
	 * @param borrow
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = {"query", ""})
	public String list(Borrow borrow, HttpServletRequest request, HttpServletResponse response, Model model){
		borrow.setCurrentDate(DateUtils.dateResetZero(new Date()));
		Page<Borrow> page = borrowService.findBorrow(new Page<>(request, response), borrow);
		model.addAttribute("page", page);
		return "modules/borrow/queryList";
	}

    /**
     * 借阅记录导出
     *
     * @param borrow
     * @param response
     */
    @RequestMapping("export")
    public void list(Borrow borrow, HttpServletResponse response){
        try{
            borrow.setCurrentDate(DateUtils.dateResetZero(new Date()));
            List<Borrow> list = borrowService.findList(borrow);

            String fileName = "借阅列表" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
            new ExportExcel("", Borrow.class).setDataList(list).write(response, fileName).dispose();
        }
        catch(Exception e){
            e.printStackTrace();
        }
    }

	@RequestMapping(value = {"readerborrowlist", ""})
	public String readerborrowlist(Borrow borrow, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Borrow> page = borrowService.findBorrow(new Page<>(request, response), borrow);
		model.addAttribute("page", page);
		return "modules/borrow/readerborrowlist";
	}

	@RequestMapping("listData")
	@ResponseBody
	public Page<Reader> listData(Reader reader, HttpServletRequest request, HttpServletResponse response) {
		return readerService.findUser(new Page<>(request, response), reader);
	}

	/**
	 * 读者添加页面
	 *
	 * @param reader
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "form")
	public String form(Reader reader, Model model, ReaderRoler readerRoler, HttpServletRequest request, HttpServletResponse response){
		Page<ReaderRoler> page = readerRolerService.findUser(new Page<>(request, response), readerRoler);
		model.addAttribute("page", page);
		model.addAttribute("reader", reader);
		return "modules/reader/readerForm";
	}


	/**
	 * 第一次借出
	 * @param request
	 * @param response
	 * @param redirectAttributes
	 * @return
	 * @throws ParseException
	 */
	@RequestMapping(value = "save")
	public String save(HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) throws ParseException {
		String message;
		String banidlist = request.getParameter("banidlist");
		String rdId = request.getParameter("rdId");
		String placeid = request.getParameter("placeid");//书库id

		String[] banids = banidlist.split(",");
		ReaderRoler readerRoler = findrolerbyId(rdId);
		String rdCanBorrowdBooks=readerRoler.getRdCanBorrowdBooks();//可借册数
		String rdCanBorrowDays=readerRoler.getRdCanBorrowDays();//可借天数
		//判断当前用户的状态是否正常  0:正常   1：停用    2：挂失
		Reader reader = new Reader();
		reader.setRdId(rdId);
		Page<Reader> readerpage = readerService.findUser(new Page<Reader>(request, response), reader);
		String reader_status="";
		String rdName="";
		for (Reader reader1:readerpage.getList()){
			reader_status = reader1.getRdState();
			rdName = reader1.getRdName();
		}
		if ("1".equals(reader_status)){
			message = "您的卡号已经停用~请重新激活";
			addMessage(redirectAttributes, message);
			return "redirect:" + adminPath + "/book/borrowed/management?repage";
		}
		if ("2".equals(reader_status)){
			message = "您的卡号已经挂失~请重新激活";
			addMessage(redirectAttributes, message);
			return "redirect:" + adminPath + "/book/borrowed/management?repage";
		}
		//需要判断用户借出的本书是不是超出了角色的范围 用户可以借出1本，再借发生error
		int already_count =  borrowService.getBorrowCount(rdId);
		if((already_count+banids.length)>Integer.valueOf(rdCanBorrowdBooks)){
			message = "您的借阅本书已经达到上限，不能借出";
			addMessage(redirectAttributes, message);
			return "redirect:" + adminPath + "/book/borrowed/management?repage";
		}
		//该用户是不是有超期的书籍，有的话，也不能借出
		Borrow borrow = new Borrow();
		borrow.setRdId(rdId);
		borrow.setStatus("1");//借出的书籍
		Page<Borrow> page = borrowService.findBorrow(new Page<Borrow>(request, response), borrow);
		//有借出记录，需要判断有么有超期的
		if (page.getList().size() > 0) {
			for (Borrow borrow1 : page.getList()) {
				// 计算书籍应还日期
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				Date date = borrow1.getBorDate();
				String str = sdf.format(date);
				String returnday = DateUtils.dateadd(str, Integer.valueOf(rdCanBorrowDays));
				Date return_date = new SimpleDateFormat("yyyy-MM-dd").parse(returnday);//应还日期
				Date current_date = new SimpleDateFormat("yyyy-MM-dd").parse(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));//当前时间
				if(return_date.getTime() < current_date.getTime()){
					message = "'" + borrow1.getBkId() + "'" + "书籍已经超期";
					addMessage(redirectAttributes, message);
					return "redirect:" + adminPath + "/book/borrowed/management?repage";

				}
			}
		}
		//正常条件下的借出
		for(String banid : banids){
			Borrow borrow3 = new Borrow();
			borrow3.setContCnt("0");//第一次续借次数置为0
			borrow3.setBkId(banid);
			borrow3.setRdId(rdId);
			//1.生成10位的随机数
			String ZlId = RandomNumUtiles.getRandomNum(8);
			//2.如果有的话，在重新生成，直到么有重复的项
			while(!StringUtils.isBlank(borrow.getId())){
				ZlId = RandomNumUtiles.getRandomNum(8);
			}
			borrow3.setZlId(ZlId);
			//
			String currentDate = DateUtils.getDate();
			String needRtnDate = DateUtils.dateadd(currentDate, Integer.valueOf(rdCanBorrowDays));
			borrow3.setNeedRtnDate(new SimpleDateFormat("yyyy-MM-dd").parse(needRtnDate));
			borrow3.setSource("admin");
			borrowService.saveBorrow(borrow3);
			//日志记录
			BorrowLog borrowLog = new BorrowLog("0", rdId, rdName, "书籍", banid, makeBookName(banid),
					"0", DateUtils.getDateTime(), ZlId, bookStoreService.get(banid).getBkId(),
					UserUtils.getUser().getLoginName());
			borrowService.saveLog(borrowLog);
		}
		addMessage(redirectAttributes, "借出成功~");
		return "redirect:" + adminPath + "/book/borrowed/management?repage";
	}

	/**
	 * 图书借出ajax调用扫描直接借出图书
	 * @param request
	 * @param response
	 * @return
	 * @throws ParseException
	 */
	@ResponseBody
	@RequestMapping(value = "borrow")
	public String borrow(HttpServletRequest request, HttpServletResponse response) throws ParseException {
		String retFlag;
		//图书条码
		String banid = request.getParameter("banid");
		String rdId = request.getParameter("rdid");
		String placeid = request.getParameter("placeid");//书库id
		ReaderRoler readerRoler = findrolerbyId(rdId);
		List<ReaderRolePlace> bkPlaceList = readerRoler.getReaderRolePlaceList();
		boolean flag = true;
		for(ReaderRolePlace bkPlace:bkPlaceList){
			if(placeid.equals(bkPlace.getPlaceId())){
				flag = false;
				break;
			}
		}
		if(flag){
			retFlag = "NOPLACE";
			return "{\"retFlag\":\""+retFlag+"\"}";
		}
		String rdCanBorrowdBooks = readerRoler.getRdCanBorrowdBooks();//可借册数
		String rdCanBorrowDays = readerRoler.getRdCanBorrowDays();//可借天数
		//判断当前用户的状态是否正常  0:正常   1：停用    2：挂失
		Reader reader = new Reader();
		reader.setRdId(rdId);
		Page<Reader> readerpage = readerService.findUser(new Page<>(request, response), reader);
		String reader_status = "";
		String rdName = "";
		for(Reader reader1 : readerpage.getList()){
			reader_status = reader1.getRdState();
			rdName = reader1.getRdName();
		}
		if ("1".equals(reader_status)){
			retFlag = "STOPCARD";
			return "{\"retFlag\":\""+retFlag+"\"}";

		}
		if ("2".equals(reader_status)){
			retFlag = "LOSECARD";
			return "{\"retFlag\":\""+retFlag+"\"}";
		}
		//需要判断用户借出的本书是不是超出了角色的范围 用户可以借出1本，再借发生error
		int already_count =  borrowService.getBorrowCount(rdId);
		if((already_count+1)>Integer.valueOf(rdCanBorrowdBooks)){
			retFlag = "OVERBORROW";
			return "{\"retFlag\":\""+retFlag+"\"}";
		}

		//该用户是不是有超期的书籍，有的话，也不能借出
		Borrow borrow = new Borrow();
		borrow.setRdId(rdId);
		borrow.setStatus("1");//借出的书籍
		Page<Borrow> page = borrowService.findBorrow(new Page<>(request, response), borrow);
		//有借出记录，需要判断有么有超期的
		if (page.getList().size() > 0) {
			for (Borrow borrow1 : page.getList()) {
				//计算书籍应还日期
				SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
				Date date=borrow1.getBorDate();
				String str=sdf.format(date);
				String returnday = DateUtils.dateadd(str, Integer.valueOf(rdCanBorrowDays));
				Date return_date = new SimpleDateFormat("yyyy-MM-dd").parse(returnday);//应还日期
				Date current_date = new SimpleDateFormat("yyyy-MM-dd").parse(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));//当前时间
				if (return_date.getTime() < current_date.getTime()) {
					retFlag = "OVERDATE";
					return "{\"retFlag\":\""+retFlag+"\"}";
				}
			}
		}
		//正常条件下的借出
		Borrow borrow3 = new Borrow();
		borrow3.setContCnt("0");//第一次续借次数置为0
		borrow3.setBkId(banid);
		borrow3.setRdId(rdId);
		//1.生成10位的随机数
		String ZlId = RandomNumUtiles.getRandomNum(8);
		//2.如果有的话，在重新生成，直到么有重复的项
		while (!StringUtils.isBlank(borrow.getId())){
			ZlId = RandomNumUtiles.getRandomNum(8);
		}
		borrow3.setZlId(ZlId);
		String currentDate = DateUtils.getDate();
		String needRtnDate = DateUtils.dateadd(currentDate, Integer.valueOf(rdCanBorrowDays));
		borrow3.setNeedRtnDate(new SimpleDateFormat("yyyy-MM-dd").parse(needRtnDate));
		borrow3.setSource("admin");
		borrowService.saveBorrow(borrow3);
		//日志记录
		BorrowLog borrowLog = new BorrowLog("0",rdId,rdName,"书籍",banid,makeBookName(banid),"0",
				DateUtils.getDateTime(),ZlId,bookStoreService.get(banid).getBkId(),
				UserUtils.getUser().getLoginName());

		borrowService.saveLog(borrowLog);

		retFlag = "SUCCESS";
		return "{\"retFlag\":\"" + retFlag + "\"}";
	}

	/**
	 * 续借
	 *
	 * @param request
	 * @param response
	 * @param redirectAttributes
	 * @return
	 */
	@RequestMapping(value = "brcontinue")
	public String brcontinue(HttpServletRequest request, HttpServletResponse response,
							 RedirectAttributes redirectAttributes){
		String message;
		//根据借阅记录号，查询这条记录
		String zlId = request.getParameter("id");
		Borrow borrow= borrowService.findOne(zlId);
		SimpleDateFormat sdf =  new SimpleDateFormat("yyyy-MM-dd");
		//(borrow.getZlId()更新的记录   DateUtils.getDate()借出的日期  UserUtils.getUser()当前操作人员  "1" 归还
		//borrowService.savebrcontinue(borrow.getZlId(), DateUtils.getDate(), UserUtils.getUser().getLoginName(),"1");
		//1.查询用户的角色，根据角色查询续借的信息，天数，本书，  可以借的本书和天数
		String rdId =  borrow.getRdId();//读者编号
		//根据读者编号，查询读者角色编号，根据角色编号，查询角色可以续借的本书，天数等等
		ReaderRoler readerRoler= findrolerbyId(rdId);
		String rdCanBorrowDays=readerRoler.getRdCanBorrowDays();//可借天数
		String rdCanBorrowdBooks=readerRoler.getRdCanBorrowdBooks();//可借册数
		String rdContinueBorrowCount=readerRoler.getRdContinueBorrowCount();//可续借次数
		String rdContinueBorrowDays=readerRoler.getRdContinueBorrowDays();//可续借天数
		//2.续借的开始日期  假如30号到期，25号续借的，续借是从30号算
		SimpleDateFormat sdf11=new SimpleDateFormat("yyyy-MM-dd");
		Date date=borrow.getNeedRtnDate();
		String str=sdf11.format(date);
		String continuday =str;
		String continurtnday = DateUtils.dateadd(str, Integer.valueOf(rdContinueBorrowDays));//续借的开始日期=上次借出的日期+可以续借的天数
		int count=0;
		count = Integer.valueOf(borrow.getContCnt())+1;//续借次数
		//需要判断续借的次数和本书要满足续借的条件，即不能大于满足的条件
		if(Integer.valueOf(rdContinueBorrowCount)<count){
			message = "您已经超出续借次数";
			addMessage(redirectAttributes, message);
			return "redirect:" + adminPath + "/book/borrowed/query?repage";
		}
		//满足条件即可续借  续借次数为0插入  续借次数>0 更新
		if (Integer.valueOf(borrow.getContCnt())==0){
			borrowService.savebrcontinue(borrow.getZlId(), continuday,continurtnday, UserUtils.getUser().getLoginName(),"1",count);
		}else {
			borrowService.updateCont(borrow.getZlId(), continuday, continurtnday, UserUtils.getUser().getLoginName(), "1", count);
		}
		//日志记录
		BorrowLog borrowLog = new BorrowLog("2", borrow.getRdId(), readerService.getUser(borrow.getRdId()).getRdName(), "书籍",
				borrow.getBkId(), makeBookName(borrow.getBkId()), "0", DateUtils.getDateTime(),
				borrow.getZlId(), bookStoreService.get(borrow.getBkId()).getBkId(), UserUtils.getUser().getLoginName());
		borrowService.saveLog(borrowLog);
		addMessage(redirectAttributes, "续借成功~");
		return "redirect:" + adminPath + "/book/borrowed/query?repage";
	}

	/**
	 * 根据读者编号rdId查询角色权限
	 *
	 * @param rdId
	 * @return
	 */
	@RequestMapping(value = "findrolerbyId")
	public ReaderRoler findrolerbyId(String rdId){
		Reader reader = new Reader();
		reader.setRdId(rdId);
		return readerService.findReaderRoleByRdid(reader);
	}

	/**
	 * 归还首页
	 * @return
	 */
	@RequestMapping(value = {"rtnbooklist", ""})
	public String rtnbooklist() {
		return "modules/borrow/returnForm";
	}

	/**
	 * 根据读者条码获取书籍列表
	 *
	 * @param borrow
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "rtnbookbyrdid", method = RequestMethod.POST)
	public @ResponseBody
	String rtnbookbyrdid(Borrow borrow, HttpServletRequest request, HttpServletResponse response){
		Page<Borrow> page = borrowService.rtnbooklist(new Page<>(request, response), borrow, request.getParameter("rdCardno"));
		return rtnjson(page, "success");
	}

    /**
     * 根据书籍条码获取读者和书籍的信息,并进行图书归还（非jsonlist）
     *
     * @param banid
     * @return String
	 */
	@ResponseBody
	@RequestMapping(value = "returnBookByBanid", method = RequestMethod.POST)
	public String returnBookByBanid(String banid){
		Borrow borrow = borrowService.findBorrowListByBanid(banid);
		if(null == borrow){
			return null;
		}
		else{

			Result result = borrowService.return_book(banid);
			if(Result.SUCCESS.equals(result.getCode())){
				return "{\"retFlag\":\"success\"}";
			}
			else{
				return "{\"retFlag\":\"" + result.getMsg() + "\"}";
			}
		}
    }

	/**
	 * ajax请求返回结果处理 只返回一条信息
	 * @param
	 * @return
	 */
	public Reback getReback(Borrow borrow,String retFlag){
		if(borrow==null){
			return null;
		}
			Reback reback = new Reback();
			reback.setZlId(borrow.getZlId());
			reback.setBkId(borrow.getBkId());
			reback.setRdId(borrow.getRdId());
			reback.setBanid(borrow.getBkBookstore().getBanid());
			reback.setBorrowStatus(borrow.getReturnStatus());
			reback.setContCnt(borrow.getContCnt());
			reback.setRdCanBorrowDays(borrow.getReaderRoler().getRdCanBorrowDays());
			reback.setRdCanBorrowdBooks(borrow.getReaderRoler().getRdCanBorrowdBooks());
			reback.setRdContinueBorrowCount(borrow.getReaderRoler().getRdContinueBorrowCount());
			reback.setRdContinueBorrowDays(borrow.getReaderRoler().getRdContinueBorrowDays());
			reback.setRdName(borrow.getReader().getRdName());
			reback.setRdTele(borrow.getReader().getRdTele());
			reback.setStatus(borrow.getReader().getRdState());
			reback.setRdCardno(borrow.getReader().getRdCardno());
			//将副标题也进行显示
			if(StringUtils.isNotEmpty(borrow.getBkBookinfo().getOthername())){
				reback.setBookname(borrow.getBkBookinfo().getBookname()+"-"+borrow.getBkBookinfo().getOthername());
			} else {
				reback.setBookname(borrow.getBkBookinfo().getBookname());
			}
			reback.setIsbn(borrow.getBkBookinfo().getIsbn());
			reback.setRetFlag(retFlag);//返回Flag
		return reback;
	}
	/**
	 * ajax请求返回结果处理 ---构造新的对象,减少前台页面遍历的次数
	 * @param page
	 * @return
	 */
	public String rtnjson(Page<Borrow> page,String retFlag){
		//根据读者编号  查询没有结果时候，直接返回空值，在页面进行判断~~
		if(page.getList().size()==0){

			return "";
		}
		List<Reback> list = new ArrayList<>();
		for (Borrow borrow1:page.getList()){
			Reback reback = new Reback();
			reback.setZlId(borrow1.getZlId());
			reback.setBkId(borrow1.getBkId());
			reback.setRdId(borrow1.getRdId());
			reback.setBanid(borrow1.getBkBookstore().getBanid());
			reback.setBorrowStatus(borrow1.getReturnStatus());
			reback.setContCnt(borrow1.getContCnt());
			reback.setRdCanBorrowDays(borrow1.getReaderRoler().getRdCanBorrowDays());
			reback.setRdCanBorrowdBooks(borrow1.getReaderRoler().getRdCanBorrowdBooks());
			reback.setRdContinueBorrowCount(borrow1.getReaderRoler().getRdContinueBorrowCount());
			reback.setRdContinueBorrowDays(borrow1.getReaderRoler().getRdContinueBorrowDays());
			reback.setRdName(borrow1.getReader().getRdName());
			reback.setRdTele(borrow1.getReader().getRdTele());
			reback.setStatus(borrow1.getReader().getRdState());
			reback.setRdCardno(borrow1.getReader().getRdCardno());
			//将副标题也进行显示
			if(StringUtils.isNotEmpty(borrow1.getBkBookinfo().getOthername())){
				reback.setBookname(borrow1.getBkBookinfo().getBookname()+"-"+borrow1.getBkBookinfo().getOthername());
			} else {
				reback.setBookname(borrow1.getBkBookinfo().getBookname());
			}
			reback.setIsbn(borrow1.getBkBookinfo().getIsbn());
			reback.setRetFlag(retFlag);//返回Flag
			list.add(reback);
		}
		return Object2Json(list);
	}

	/**
	 * Java对象转JSon（String）
	 * @param obj
	 * @return
	 */
	public String Object2Json(Object obj){
		ObjectMapper mapper = new ObjectMapper();
		String Json = null;
		try {
			Json = mapper.writeValueAsString(obj);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
		System.out.println("Change Object to JSON String: " + Json);
		return Json;
	}

	/**
	 * 归还
	 * @param request
	 * @param redirectAttributes
	 * @return
	 */
	@RequestMapping(value = "rtnbook")
	public String rtnbook(HttpServletRequest request, RedirectAttributes redirectAttributes){
		String bookIds = request.getParameter("bookIds");
		String[] zlId = bookIds.split(",");
		for(String s : zlId){
			//根据借阅记录号，查询这条记录
			Borrow borrow = borrowService.findOne(s);

			//(borrow.getZlId()更新的记录   DateUtils.getDate()借出的日期  UserUtils.getUser()当前操作人员  "1" 归还
			//如果续借次数>0 说明有续借的记录，这时候应该同步一下续借的记录，将该条的记录置为无效0，否则直接归还
			if(Integer.valueOf(borrow.getContCnt()) > 0){
				borrowService.updateContinuestatus(borrow.getZlId(), "0");
			}
			borrowService.saveback(borrow.getZlId(), DateUtils.getDate(), UserUtils.getUser().getLoginName(), "0");
			addMessage(redirectAttributes, "归还成功~");
			//日志记录
			BorrowLog borrowLog = new BorrowLog("1", borrow.getRdId(), readerService.getUser(borrow.getRdId()).getRdName(),
					"书籍", borrow.getBkId(), makeBookName(borrow.getBkId()), "0",
					DateUtils.getDateTime(), borrow.getZlId(),
					bookStoreService.get(borrow.getBkId()).getBkId(), UserUtils.getUser().getLoginName());
			borrowService.saveLog(borrowLog);
		}
		return "redirect:" + adminPath + "/book/borrowed/query?repage";
	}

	/**
	 * 当前流通
	 *
	 * @param borrow
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = {"currentcirculationlist"})
	public String currentCirculationList(Borrow borrow, HttpServletRequest request, HttpServletResponse response, Model model){
		borrow.setCurrentDate(DateUtils.dateResetZero(new Date()));
		Page<Borrow> page = borrowService.findCurrentCirculation(new Page<>(request, response), borrow);
		model.addAttribute("page", page);
		return "modules/borrow/currentCirculationList";
	}

	/**
	 * 构造bookname+“-”+othername的格式
	 *
	 * @param bkid
	 * @return
	 */
	private String makeBookName(String bkid){
		BkBookstore bkBookstore = bookStoreService.get(bkid);
		String bookName = bkBookstore.getBookname();
		String otherName = bkBookstore.getOthername();
		if(StringUtils.isNotEmpty(otherName)){
			return bookName + "-" + otherName;
		}
		else{
			return bookName;
		}
	}
}
